package pl.polsl.mmm.projectcomm.programs;

import static android.opengl.GLES20.GL_TEXTURE0;
import static android.opengl.GLES20.GL_TEXTURE_2D;
import static android.opengl.GLES20.glActiveTexture;
import static android.opengl.GLES20.glBindTexture;
import static android.opengl.GLES20.glGetAttribLocation;
import static android.opengl.GLES20.glGetUniformLocation;
import static android.opengl.GLES20.glUniform1i;
import static android.opengl.GLES20.glUniformMatrix4fv;
import static android.opengl.GLES20.glValidateProgram;

import android.content.Context;

import pl.polsl.mmm.projectcomm.R;
public class TextureShaderProgram extends ShaderProgram{
	//uniforms
	private final int uMatrixLocation;
	private final int uTextureUnitLocation;
    //private final int uColorLocation; //tylko debug
	//attributes
	private final int aPositionLocation;
	private final int aTextureCoordinatesLocation;
    private final int aNormalsLocation;
	
	public TextureShaderProgram(Context context) {
		super(context, R.raw.texture_vertex_shader, R.raw.texture_fragment_shader);
		
		//Retrieve uniform locations for the shader program.
		uMatrixLocation = glGetUniformLocation(program, U_MATRIX);
		uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_UNIT);
        //kolor debug
        //uColorLocation = glGetUniformLocation(program, U_COLOR);

		//Retrieve attribute locations for the shader program.
		aPositionLocation = glGetAttribLocation(program, A_POSITION);
        aNormalsLocation = glGetAttribLocation(program, A_NORMAL);
		aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES);


        glValidateProgram(program);
	}

	public void setMatrix(float[] matrix) {
		// przekazanie macierzy do programu shadera
		glUniformMatrix4fv(uMatrixLocation, 1, false, matrix, 0);
	}
    public void setTexture(int textureId) {
        // aktywna tekstura: tekstura 0
        glActiveTexture(GL_TEXTURE0);
        // powiązanie naszej tekstury z wybrana wczesniej aktywną jednostką
        glBindTexture(GL_TEXTURE_2D, textureId);
        // wpisanie tekstury do zmiennej uniform shadera
        glUniform1i(uTextureUnitLocation,0);
    }
	public int getPositionAttributeLocation() {
		return aPositionLocation;
	}
	
	public int getTextureCoordinatesAttributeLocation() {
		return aTextureCoordinatesLocation;
	}

    public int getNormalsAttributeLocation() {
        return aNormalsLocation;
    }
}
